//
//  CFolderMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/4/9.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CFolderMgr.h"
#import "MicroDef.h"

@interface CFolderMgr()
{
    
}

@end


@implementation CFolderMgr
+(CFolderMgr *)shareInstance
{
    static CFolderMgr *instance  = nil;
    static dispatch_once_t dispatch_onces;
    if(instance  == nil)
    {
        dispatch_once(&dispatch_onces, ^{
            
            instance = [[CFolderMgr alloc] init];
        });
    }
    return instance;
}

-(id)init
{
    self = [super init];
    if(self)
    {
        _accMgr = [[CAccountMgr alloc] init];
        _serSettingMgr = [[CServerSettingMgr alloc] init];
        _recentlyAcc = nil;
    }
    return self;
}



-(void)resetRecentlyAccount
{
    _recentlyAcc = nil;
}



-(NSString *)getDBPathByAccount:(NSString *)account
{
    return [self personalDatabasePath:account];
}

static NSString *const singleDatabasePath = @"/DB/wanzhaoImPersonal.db";
- (NSString *)personalDatabasePath:(NSString *)account
{
    CServerSettingModel *serModel = [[CFolderMgr shareInstance].serSettingMgr getSetting];
    if (serModel == nil) {
        return nil;
    }
    if(M_ISSTRNULL(account) || M_ISSTRNULL(serModel.companyId) || M_ISSTRNULL(serModel.IMIp))
    {
        return nil;
    }
    
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//    NSString *plistPath = [paths objectAtIndex:0];
    NSString *plistPath = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.com.wanzhao"] path];
    NSString *fullPath = [NSString stringWithFormat:@"%@/%@/%@/%@%@", plistPath,IMIP,serModel.companyId, account,singleDatabasePath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:fullPath])
    {
        [fileManager createDirectoryAtPath:[fullPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return fullPath;
}


- (NSString *)personalDatabasePathRecently
{
    _recentlyAcc = [_accMgr getRecentlyLoginAccount];
    
    
    if(!M_ISSTRNULL(_recentlyAcc.userName))
    {
//        NSLog(@"%@",[self getDBPathByAccount:_recentlyAcc.userName]);
        return [self getDBPathByAccount:_recentlyAcc.userName];
    }
    
    return nil;
}


static NSString *const commonDatabasePath = @"/DB/wanzhaoImCommon.db";
- (NSString *)commonDatabasePath
{
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//    NSString *plistPath = [paths objectAtIndex:0];
    NSString *plistPath = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.com.wanzhao"] path];
    NSString *fullPath = [NSString stringWithFormat:@"%@/%@/%@%@", plistPath,IMIP, @"Common",commonDatabasePath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:fullPath])
    {
        [fileManager createDirectoryAtPath:[fullPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return fullPath;
}

- (NSString *)companyCommonDatabasePath
{
    
    CServerSettingModel *serModel = [[CFolderMgr shareInstance].serSettingMgr getSetting];
    
//    NSLog(@"11111111");
    if (serModel == nil) {
        return nil;
    }

    if (M_ISSTRNULL(serModel.companyId) || M_ISSTRNULL(serModel.IMIp)) {
        return nil;
    }
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//    NSString *plistPath = [paths objectAtIndex:0];
    NSString *plistPath = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.com.wanzhao"] path];
    NSString *fullPath = [NSString stringWithFormat:@"%@/%@/%@/%@%@", plistPath, IMIP,serModel.companyId, @"Common",commonDatabasePath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:fullPath])
    {
        [fileManager createDirectoryAtPath:[fullPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return fullPath;

}

@end
